package com.game.message.jpb;

import com.game.message.Msg;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * Created by legend9999 on 2016/6/26.
 */
@Deprecated
public class JPbMsg extends Msg {
    private ByteBuf byteBuf;
    private int msgId = 0;
    public JPbMsg(int msgId) {
        byteBuf = Unpooled.buffer(8);
        this.msgId = msgId;
    }

    public int getMsgId() {
        return msgId;
    }

    public ByteBuf encode(byte[] body) {
        int bodyLength = body.length;
        int sumLength = 8 + bodyLength;
        this.byteBuf.writeInt(sumLength);
        this.byteBuf.writeInt(msgId);
        boolean writable = byteBuf.isWritable(body.length);
        if (!writable) {
            byteBuf.capacity(bodyLength);
        }
        return this.byteBuf.writeBytes(body);
    }
    public byte[] decode() {
        int sumLength = byteBuf.readInt();
        int msgId = byteBuf.readInt();
        int readableLength = byteBuf.readableBytes();
        byte[] body  = new byte[readableLength];
        for (int i1 = 0; i1 < readableLength; i1++) {
            body[i1] = byteBuf.readByte();
        }
        return body;
    }

    public ByteBuf getByteBuf() {
        return byteBuf;
    }
}
